package com.gfm.story.service.story;

import com.gfm.story.dao.BookDao;
import com.gfm.story.dao.CategoryDao;
import com.gfm.story.dao.RecommendDao;
import com.gfm.story.entity.base.ResultBean;
import com.gfm.story.entity.story.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @BelongsProject: story
 * @BelongsPackage: com.gfm.story.service.story
 * @Author: Administrator
 * @CreateTime: 2019-06-04 09:53
 * @Description: ${Description}
 */
@Service
public class RecommendService {
    @Resource
    private RecommendDao recommendDao;
    @Resource
    private BookDao bookDao;

    @Resource
    private CategoryDao categoryDao;





//    //添加用户推荐记录：一个用户一天只能对一本小说推荐一次
//    public boolean addRecommendLog(Long userId,Long bookId){
//        int count = recommendDao.getUserRecommendLog(userId,bookId);
//        if (count>=1){
//            return false;
//        }
//        //2.增加点击记录
//        Recommend recommend = new Recommend();
//        recommend.setBookId(bookId);
//        recommend.setUserId(userId);
//        recommend.setRecommendTime(new Date());
//        if (recommend.insert()){
//            return true;
//        }
//        return false;
//    }


    //推荐榜
    public Map<String,Object> getBookListByDay(Map<String,Object> map,Integer page){
        //1.查找当日内所有的存在推荐记录的书id
        List<Recommend> listReadLog = recommendDao.getBookListByDay(map);
        List<Book> books = new ArrayList<Book>();
        List<Long> ids = new ArrayList<Long>();
        for (Recommend recommend:listReadLog){
            ids.add(recommend.getBookId());
        }
        map.put("ids", ids);
        Map<String,Object> map1 = new HashMap<String,Object>();
        List<Book> list = new ArrayList<Book>();
        Integer count = 0;
        if (!listReadLog.isEmpty()){
            //分页查询
            list = recommendDao.getReadPhPage(map);
            for (Book book:list){
                map.put("bookId",book.getId());
                int count2 = recommendDao.getBookCountByDay(map);
                book.setCollectionCount(count2);
                //修改图书类型
                if (book.getCategoryId()!=null){
                    Category category = new Category();
                    category.setId(book.getCategoryId());
                    Category category1 = categoryDao.selectOne(category);
                    book.setCategoryName(category1.getName());
                }else{
                    book.setCategoryName("其他小说");
                }
                books.add(book);
            }
            count = recommendDao.getReadPhCount(map);
        }
        Collections.sort(books,new Comparator<Book>(){
            @Override
            public int compare(Book o1, Book o2) {
                int i = o1.getCollectionCount() - o2.getCollectionCount();
                if (i>0){
                    return 1;
                }else if(i<0){
                    return -1;
                }
                return 0;//相等
            }
        });
        Collections.reverse(books);
        map1.put("total", (count+20-1)/20);
        map1.put("currentPage", page);
        map1.put("objs", books);
        return map1;
    }

    //添加推荐
    public boolean addOrEdit(Long bookId, Long userId) {
        //1.查询用户在当日内对同一本数是否已有点击记录
        int count = recommendDao.getUserRecommendLog(userId,bookId);
        if (count>=1){
            return false;
        }
        //2.增加点击记录
        Recommend recommend = new Recommend();
        recommend.setBookId(bookId);
        recommend.setUserId(userId);
        recommend.setRecommendTime(new Date());
        if (recommend.insert()){
            return true;
        }
        return false;
    }
}
